package com.main.demo;

import java.util.Arrays;
import java.util.Scanner;

/**
 * @author admin
 * @version 1.0
 * @date 2019/5/30
 */
public class Bank {
    private BandCard[] bandCards;
    private int usedSize;

    public Bank() {
        this(10);
    }

    public Bank(int size) {
        System.out.println("初识化账户" + size);
        this.bandCards = new BandCard[size];
        /*初识长度为0*/
        this.usedSize = 0;
    }

    /*用户登录*/
    public BandCard login(int id, String password) {
        if (bandCards.length != 0) {
            for (int i = 0; i < this.bandCards.length; i++) {
                if (bandCards[i].getId() == id && bandCards[i].getPassword().equals(password)) {
                    System.out.println("登录成功！账号id:" + id);
                    return bandCards[i];
                }
            }
        }
        System.out.println("登录失败！您可能还未注册或者是密码和账号有误！账号id：" + id);
        return null;
    }

    /*用户注册*/
    public void register(int id, String password) {
        for (int i = 0; i < this.usedSize; i++) {
            if (bandCards[i].getId() == id) {
                System.out.println("您已经注册过了! 账号id：" + id);
                return;
            }
        }
        /*查看当前银行卡数组是否已经注册满，满了进行扩容*/
        if (isFull()) {
            incBankCard();
        }
        this.bandCards[this.usedSize++] = new BandCard(id, password);
    }

    //原数组的2倍扩容
    private void incBankCard() {
        this.bandCards = Arrays.copyOf(this.bandCards, this.bandCards.length * 2);
    }

    private boolean isFull() {
        return this.usedSize == this.bandCards.length;
    }

    /*登录成功*/
    private void loginSuccess(Person person, int n, BandCard card) {
        switch (n) {
            case 1:
                person.save(card);
                break;
            case 2:
                person.draw(card);
                break;
            case 3:
                person.getBalance(card);
                break;
            case 4:
                person.transfer(card, this.bandCards, this.usedSize);
                break;
            default:
                break;
        }
    }

    public void operate(Person person) {
        int count = 0;
        while (count <= 3) {
            System.out.println("欢迎登录模拟ATM系统");
            System.out.println("1.登陆  2.注册  3.退出");
            Scanner scanner = new Scanner(System.in);
            int n = scanner.nextInt();
            if (n == 3) {
                return;
            }
            System.out.println("请输入账号：");
            int id = scanner.nextInt();
            System.out.println("请输入密码：");
            String passwd = scanner.next();
            switch (n) {
                case 1:
                    BandCard card = login(id, passwd);
                    if (card != null) {
                        while (true) {
                            System.out.println("========请输入你的操作========");
                            System.out.println("1 存钱  2 取钱   3 查看余额  4 转账 5 退卡");
                            n = scanner.nextInt();
                            if (n == 5) {
                                break;
                            }
                            loginSuccess(person, n, card);
                        }
                    } else {
                        System.out.println("登录失败！");
                        count++;
                    }
                    break;
                case 2:
                    register(id, passwd);
                    break;
                default:
                    System.out.println("输入指令有误，请按照上方规则输入！");
                    break;
            }
        }
    }

}
